//
//
//  @ Project : AgriMarketSystem
//  @ Date : 18/10/2013
//  @ Author : NTT
//
//
using System.Linq;
using System.Collections.Generic;
using System;
namespace AgriMarketService
{
    public class TypeOfAuction
    {
        private int typeId;

        public int TypeId
        {
            get { return typeId; }
            set { typeId = value; }
        }
        private string typeName;

        public string TypeName
        {
            get { return typeName; }
            set { typeName = value; }
        }
        public TypeOfAuction() { }
        public static TypeOfAuction GetTypeById(int id)
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            return (from tempType in db.Tbl_TypeOfAuctions
                    where tempType.TypeId == id
                    select new TypeOfAuction(){ TypeId=tempType.TypeId,TypeName=tempType.Type}).FirstOrDefault();
        }
        public static List<TypeOfAuction> GetAllTypes()
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            return (from tempType in db.Tbl_TypeOfAuctions
                    select new TypeOfAuction() { TypeId = tempType.TypeId, TypeName = tempType.Type }).ToList();
        }
        public static int UpdateTypeOfAuction(TypeOfAuction updated) {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            Tbl_TypeOfAuction updatedTbl_TypeOfAuction = (from temp in db.Tbl_TypeOfAuctions
                                                          where temp.TypeId == updated.typeId
                                                          select temp).FirstOrDefault();
            if (updatedTbl_TypeOfAuction == null)
                return -1;//no exist

            updatedTbl_TypeOfAuction.TypeId = updated.TypeId;
            updatedTbl_TypeOfAuction.Type = updated.TypeName;
            try
            {
                db.SubmitChanges();
                return 0;
            }
            catch (Exception e)
            {
                return 1;//error
            }
        }
        public static int DeleteTypeOfAuctionById(int typeId)
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            Tbl_TypeOfAuction deletedType = (from temp in db.Tbl_TypeOfAuctions
                                             where temp.TypeId == typeId
                                                          select temp).FirstOrDefault();
            if (deletedType == null)
                return -1;//no exist
            var auctions = (from temp in db.Tbl_Auctions where temp.TypeId == typeId select temp);
            foreach (var item in auctions)
            {
                if (Auction.DeleteByAuctionId(item.AuctionId) == -1)
                    return 1;
            }
            db.Tbl_TypeOfAuctions.DeleteOnSubmit(deletedType);
            try
            {
                db.SubmitChanges();
                return 0;
            }
            catch (Exception e)
            {
                return 1;//error
            }
        }
        public static int AddNewTypeOfAuction(TypeOfAuction newType)
        {
            Tbl_TypeOfAuction newTblType = new Tbl_TypeOfAuction() { Type=newType.TypeName };
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            db.Tbl_TypeOfAuctions.InsertOnSubmit(newTblType);
            try
            {
                db.SubmitChanges();
                newType.TypeId = newTblType.TypeId;
                return newTblType.TypeId;
            }
            catch (Exception e)
            {
                return -1;//error
            }
        }
    }
}